package DynamicProgramming.Medium;

public class LC0279 {
    public int numSquares(int n) {
        int[] dp = new int[n + 1]; // dp[i]表示合成i所需完全平方数的最小数量
        for (int i = 1; i <= n; i++) {
            dp[i] = n + 1;
        }
        for (int i = 1; i * i <= n; i++) {
            if (i * i == n) return 1;
            dp[i * i] = 1;
        }

        for (int i = 2; i <= n; i++) {
            for (int j = 1; j * j <= i; j++) {
                dp[i] = Math.min(dp[i], dp[i - j * j] + 1);
            }
        }

        return dp[n];
    }
}
